<?php
namespace app\admin\controller;
use pidan\App;
use pidan\facade\Request;
use app\admin\Controller;
use app\admin\model\Seller as SellerModel;
use pidan\facade\Db;

class Seller extends Controller
{
	protected function initialize()
	{
		$this->model=new SellerModel;
	}
	public function list(){
		$req=$this->request;
		$table=$this->model;
		//查询字段
		$data=$req->only(['email','nickname'],'get','trim');
		foreach($data as $k=>$v){
			if(empty($v))  unset($data[$k]);
		}
		//要显示的
		$filed=explode(',','id,nickname,email,gender,money,coin,createtime,logintime,loginnum,status');

		$sort_by=input('request.sort_by','id','trim');
		$sort_order=input('request.sort_order','asc','trim');

		$page=input('request.page',1,'intval');
		$limit=input('request.limit',30,'intval');

		$list= $table->field($filed);

		$data['deletetime']=0;

		$count=$list->count('id');
		$list=$list->where($data)->order($sort_by, $sort_order)->limit($limit)->page($page)->select()->toArray();
		return $this->lists($list,$count);
	}
	public function batchdel(){
		$req=$this->request;
		$table=$this->model;

		$ids=input('request.ids',0,'intval');

		Db::startTrans();
		try {
			SellerModel::destroy($ids);
			$this->adminLog('批量删除商家:'.implode(',',$ids));
			$result=['code'=>0,'msg'=>'操作成功'];
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			$result=['code'=>1,'msg'=>$e->getMessage()];
		}
		return $this->result($result);
	}
	public function del(){
		$req=$this->request;
		$table=$this->model;

		$id=input('request.id',0,'intval');

		Db::startTrans();
		try {
			$table=SellerModel::find($id);
			$table->delete();
			$this->adminLog('删除商家:'.$table->id.' '.$table->email);
			$result=['code'=>0,'msg'=>'操作成功'];
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			$result=['code'=>1,'msg'=>$e->getMessage()];
		}
		return $this->result($result);
	}


}
